package com.yaoxun.bored.controller.sys;

import java.util.Date;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.yaoxun.bored.common.BaseController;
import com.yaoxun.bored.common.Constant;
import com.yaoxun.bored.common.JsonObject;
import com.yaoxun.bored.common.PageResult;
import com.yaoxun.bored.model.sys.role.Role;
import com.yaoxun.bored.model.sys.role.RoleQuery;
import com.yaoxun.bored.service.sys.role.RoleService;
import com.yaoxun.bored.util.enums.JsonStatus;

import tk.mybatis.mapper.entity.Example;

@RequestMapping("/role")
@Controller
public class RoleController extends BaseController {
	
	private static final String EDIT_PAGE = "sys/role/role_edit";
	
	private static final String INDEX_PAGE = "sys/role/role";
	
	@Inject
	private RoleService roleService;
	
	
	@RequestMapping(value="/save", method=RequestMethod.GET)
	public ModelAndView save() {
		ModelAndView mv = new ModelAndView(EDIT_PAGE);
		return mv;
	}
	
	@RequestMapping(value="/save", method=RequestMethod.POST)
	@ResponseBody
	public JsonObject save(Role role) {
		JsonObject jsonObject = new JsonObject();
		if(false == commValidate(role, jsonObject)) {
			return jsonObject;
		}
		try {
			role.setCreateId(getLoginUserId());
			role.setCreateTime(new Date());
			roleService.insert(role);
			return jsonObject;
		} catch(DuplicateKeyException e) {
			jsonObject.setStatusMsg(JsonStatus.FAIL, "角色码已经存在");
			return jsonObject;
		}
	}
	
	/**
	 * 判断角色码是否已经存在
	 * @param roleCode
	 * @return
	 */
	@RequestMapping(value="/exist", method=RequestMethod.GET)
	@ResponseBody
	public String exist(@RequestParam String roleCode) {
		Example example = new Example(Role.class);
		example.createCriteria().andEqualTo("roleCode", roleCode);
		int count = roleService.selectCountByExample(example);
		if(count > 0) {
			return JsonStatus.FAIL.value();
		} else {
			return JsonStatus.SUCCESS.value();
		}
	}
	
	@RequestMapping(value="/edit", method=RequestMethod.GET)
	public ModelAndView edit(@RequestParam Integer id) {
		ModelAndView mv = new ModelAndView(EDIT_PAGE);
		Role role = roleService.selectByPrimaryKey(id);
		mv.addObject(Constant.MV_OBJ_NAME, role);
		return mv;
	}
	
	@RequestMapping(value="/edit", method=RequestMethod.POST)
	@ResponseBody
	public JsonObject edit(Role role) {
		JsonObject jsonObject = new JsonObject();
		if(false == commValidate(role, jsonObject)) {
			return jsonObject;
		}
		roleService.updateByPrimaryKeySelective(role);
		return jsonObject;
	}
	
	/**
	 * 跳转到页面
	 * @author Loren
	 * @createTime 2018年2月2日 下午6:13:17
	 * @return
	 */
	@RequestMapping(value="/index", method=RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView mv = new ModelAndView(INDEX_PAGE);
		return mv;
	}
	
	@RequestMapping("/{id}")
	@ResponseBody
	public Role role(@PathVariable("id") Integer roleId) {
		return roleService.selectOne(roleId);
	}
	
	/**
	 * 分页查询
	 * @param query
	 * @return
	 */
	@RequestMapping(value="/search")
	@ResponseBody
	public PageResult search(RoleQuery query) {
		query.setOrderBy("role_id desc");
		PageInfo<Map<String, Object>> info = roleService.selectByPaging(query);
		PageResult result = new PageResult(info.getTotal(), info.getList());
		return result;
	}
	
}
